package com.onpositive.gamemaker.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.onpositive.gamemaker.core.GameLevel;
import com.onpositive.gamemaker.visualizer.HexagonMath;
import com.onpositive.gamemaker.visualizer.ILevelVisualizer;
import com.onpositive.gamemaker.visualizer.ISpriteProvider;
import com.onpositive.gamemaker.visualizer.swt.BasicLevelVisualizer;
import com.onpositive.gamemaker.visualizer.swt.BasicSpriteProvider;

public class MapComposite extends ScrolledComposite
{
	protected Canvas mapCanvas; 
	protected ILevelVisualizer levelVisualizer;
	protected GC gc;
	protected GameLevel level;
	protected int xLevelOffset;
	protected int yLevelOffset;
	protected final ISpriteProvider spriteProvider;

	public MapComposite(Composite parent, int style, ISpriteProvider spriteProvider)
	{
		super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL | SWT.NO_BACKGROUND);
		this.spriteProvider = spriteProvider;
		mapCanvas = new Canvas(this, SWT.NO_BACKGROUND);
		gc = new GC(mapCanvas);
		levelVisualizer = new SWTLevelVisualizer(new SWTGraphicsDevice(gc), spriteProvider);
		setContent(mapCanvas);
		mapCanvas.addPaintListener(new PaintListener()
		{
			
			@Override
			public void paintControl(PaintEvent e)
			{
				Rectangle bounds = ((Control)e.widget).getBounds();
				levelVisualizer.visualizeLevel(level,e.x,e.y,e.width,e.height);
			}
		});
		mapCanvas.addControlListener(new ControlListener()
		{
			
			@Override
			public void controlResized(ControlEvent e)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void controlMoved(ControlEvent e)
			{
				Rectangle clientArea = getBounds();
				Point location = mapCanvas.getLocation();
				mapCanvas.redraw(-location.x,-location.y,clientArea.width, clientArea.height, true);
				
			}
		});
		//setMinSize(400, 400);

	    // Expand both horizontally and vertically
//	    setExpandHorizontal(true);
//	    setExpandVertical(true);
	}
	
	@Override
	public void dispose()
	{
		super.dispose();
		gc.dispose();
	}


	public void setLevel(GameLevel level)
	{
		this.level = level;
		if (spriteProvider instanceof BasicSpriteProvider)
			((BasicSpriteProvider) spriteProvider).setLevel(level);
		int cellDiameter = VisualizerSettings.getCellDiameter();
		int canvasWidth = level.getWidth() * (cellDiameter + cellDiameter / 2) - cellDiameter / 2; //We cut one cell radius to the left and one to the right, because we don't want to draw emty space
		int heightPerLine = (int) Math.round(HexagonMath.TRIANGLE_MULTIPLIER * cellDiameter / 2);
		int canvasHeight = Math.max(2,(level.getHeight() - 2)) * heightPerLine;
		setContent(mapCanvas);
		mapCanvas.setSize(canvasWidth,canvasHeight);
	}


	public GameLevel getLevel()
	{
		return level;
	}
	
}
